app.controller('InsuranceListCtrl', function ($scope, ajax, $modal, toaster, $rootScope) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.dplist = [];
	$scope.flag = false;
	$scope.queryMap = {};

	$scope.reload = $scope.page.loader('insurance/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.query = function () {
		if ($scope.dplist.selected != undefined) {
			$scope.queryMap.moduleId = $scope.dplist.selected.id;
		} else if ($scope.dplist.selected == undefined) {
			delete $scope.queryMap.moduleId;
		}
		$scope.reload(1, $scope.queryMap);
	};

	(function () {
		$scope.auths = $rootScope.loginUser.auths;
		$scope.ms = [];
		for (var i = 0; i < $scope.auths.length; i++) {
			if ($scope.auths[i] == "AUTH_BOOKING_INSURANCE") {
				$scope.ms.push(1);
			}
			if ($scope.auths[i] == "AUTH_RENT_INSURANCE") {
				$scope.ms.push(2);
			}
			if ($scope.auths[i] == "AUTH_BUS_INSURANCE") {
				$scope.ms.push(3);
			}
		}
		for (i = 0; i < $scope.ms.length; i++) {
			if ($scope.ms[i] == 1) {
				$scope.dplist.push({ id: 1, name: "订票" });
			} else if ($scope.ms[i] == 2) {
				$scope.dplist.push({ id: 2, name: "城际约租" });
			} else if ($scope.ms[i] == 3) {
				$scope.dplist.push({ id: 3, name: "跨城巴士" });
			}
		}
		if ($scope.dplist.length > 1) $scope.flag = true;

		$scope.queryMap.module__ids = $scope.ms;
	})();
	$scope.reload(1, $scope.queryMap);

	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		var modalInstance = $modal.open({
			templateUrl: 'template/public/insurance/writeModal.html?v=2ba505cfe3',
			controller: 'InsuranceModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type,
						"ms": $scope.ms
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			if (param.type == 'update') {
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'insurance/update';
				$scope.page.mod(updater, $scope.reload, $scope.queryMap);
			} else if (param.type == 'insert') {
				inserter.fields = param.data;
				inserter.resource = 'insurance/insert';
				$scope.page.add(inserter, $scope.reload, $scope.queryMap);
			}
		});
	};


	$scope.ids = [];
	$scope.checks = false;//默认未选中
	$scope.ckAll = false;//默认未选中
	$scope.choseArr = [];

	/*单选*/
	$scope.check = function (val) {
		if ($scope.ids.indexOf(val) < 0) {
			$scope.ids.push(val);
		} else {
			$scope.ids.splice($scope.ids.indexOf(val), 1);
		}
	};

	/*全选*/
	$scope.checkAll = function (c, v) {
		if (c == true) {
			$scope.checks = true;
			$scope.choseArr = v;
			//添加所选Id
			for (var i = 0; i < $scope.choseArr.length; i++) {
				$scope.ids.push($scope.choseArr[i].id);
			}
		} else {
			$scope.checks = false;
			$scope.ids = [];
		}
	};


	//删除
	$scope.del = function (size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
		modalInstance.result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'insurance/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload, $scope.queryMap);
			} else if (param.type == 'deleteAll') {
				if ($scope.ids == "" || $scope.ids == null) {
					toaster.pop('warning', '提示', '请至少选择一条要删除的信息！');
					return;
				}
				deleter.datalist = $scope.ids;
				deleter.resource = 'insurance/batch/delete';
				$scope.page.del(deleter, $scope.reload, $scope.queryMap).then(function () { $scope.ids = null; });
			}
		});
	};

});
